﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RedSwimmer_DevExpress
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            //var table = GetData(int.Parse(txtJailID.Text), DateTime.Parse(txtFromDateTime.Text), DateTime.Parse(txtToDateTime.Text));

            var data = GetList(int.Parse(txtJailID.Text), DateTime.Parse(txtFromDateTime.Text), DateTime.Parse(txtToDateTime.Text));

            
        }

        private DataTable GetData(int jailID, DateTime fromDateTime, DateTime toDateTime)
        {
            DataTable dt = new DataTable();

            // Open the connection
            using (SqlConnection cnn = new SqlConnection("server=76.12.182.116;Initial Catalog=EZ_DEVEL;uid=EZ@Web;Password=Qw4eb123"))
            {
                cnn.Open();

                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = cnn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "ez_Payments_GetKioskUtilizationTotals";
                    cmd.Parameters.Add(new SqlParameter { ParameterName = "JailID", DbType = DbType.Int32, Value = jailID });
                    cmd.Parameters.Add(new SqlParameter { ParameterName = "FromDate", DbType = DbType.DateTime, Value = fromDateTime });
                    cmd.Parameters.Add(new SqlParameter { ParameterName = "ToDate", DbType = DbType.DateTime, Value = toDateTime });

                    // Define the data adapter and fill the dataset
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        da.Fill(dt);
                    }
                }
            }

            return dt;
        }

        private List<Util> GetList(int jailID, DateTime fromDateTime, DateTime toDateTime)
        {
            List<Util> resultList = new List<Util>();

            // Open the connection
            using (SqlConnection cnn = new SqlConnection("see email"))
            {
                cnn.Open();

                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = cnn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "ez_Payments_GetKioskUtilizationTotals";
                    cmd.Parameters.Add(new SqlParameter { ParameterName = "JailID", DbType = DbType.Int32, Value = jailID });
                    cmd.Parameters.Add(new SqlParameter { ParameterName = "FromDate", DbType = DbType.DateTime, Value = fromDateTime });
                    cmd.Parameters.Add(new SqlParameter { ParameterName = "ToDate", DbType = DbType.DateTime, Value = toDateTime });
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        resultList.Add(new Util
                        {
                            KioskGuid   = reader["KioskGuid"].ToString(),
                            Description = reader["Description"].ToString(),
                            Utilization = decimal.Parse(reader["Utilization"].ToString()),
                            RecordCount = int.Parse(reader["RecordCount"].ToString()),
                            CreditTotal = decimal.Parse(reader["CreditTotal"].ToString()),
                            CashTotal   = decimal.Parse(reader["CashTotal"].ToString()),
                            CheckTotal  = decimal.Parse(reader["CheckTotal"].ToString()),
                            RemoteTotal = decimal.Parse(reader["RemoteTotal"].ToString()),
                            Total = decimal.Parse(reader["Total"].ToString()),
                        });
                    }
                }
            }

            return resultList;
        }
    }
    

    public class Util
    {
        public string KioskGuid   { get; set; }
        public string Description { get; set; }
        public decimal Utilization { get; set; }
        public int RecordCount { get; set; }
        public decimal CreditTotal { get; set; }
        public decimal CashTotal   { get; set; }
        public decimal CheckTotal  { get; set; }
        public decimal RemoteTotal { get; set; }
        public decimal Total { get; set; }
    }
}
